package server_agent

import (
	"lark/conf"

	"github.com/pkg/errors"
	"github.com/zeast/logs"
)

const (
	newFlag uint32 = iota
	runningFlag
	closedFlag
)

var logger *logs.Logger

func LoggerLevel() string {
	return logger.LogLevelStr()
}

func SetLoggerLevel(lv string) {
	logger.SetLogLevelStr(lv)
}

func initLogger() error {
	w, err := logs.NewFileWriter(logs.FileConfig{
		Name: conf.ServerAgentLogFileName,
		Perm: conf.ServerAgentLogPerm,
		Rotate: &logs.Rotate{
			MaxSize: conf.ServerAgentLogMaxSize,
			MaxDays: conf.ServerAgentLogMaxDays,
			Perm:    conf.ServerAgentLogRotatePerm,
			Daily:   conf.ServerAgentLogDaily,
		},
	})

	if err != nil {
		return errors.WithStack(err)
	}

	logger = logs.NewLogger(w)
	logger.LogFuncCall(true)
	logger.LogFuncCallDepth(4)
	logger.SetLogLevelStr(conf.ClientAgentLogLevel)
	logger.SetTimeLayout("2006-01-02 15:04:05.000")

	return nil
}

func SetLogger(l *logs.Logger) {
	logger = l
}
